package com.mall.order.services;


import com.mall.order.OrderQueryService;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.AddressMapper;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.shopping.constants.ShoppingRetCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Transactional(rollbackFor = Exception.class)
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;
    @Autowired
    OrderConverter orderConverter;
    @Autowired
    AddressMapper addressMapper;

    /**
     * 获取用户的所有订单信息
     * @param request
     * @return
     */
    @Override
    public OrderListResponse getUserAllOrders(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();
        try{

            List<OrderDetailInfo> orderDetails
                    = orderMapper.getOrderDetailByUserId(request.getUserId(), request.getSize(), request.getPage());
            for (OrderDetailInfo orderDetail : orderDetails) {
                Example example = new Example(OrderItem.class);
                example.and().andEqualTo("orderId",orderDetail.getOrderId());
                List<OrderItem> orderItems = orderItemMapper.selectByExample(example);

                List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
                orderDetail.setOrderItemDto(orderItemDtos);
            }
            Example example = new Example(Order.class);
            example.and().andEqualTo("userId", request.getUserId());
            int total = orderMapper.selectCountByExample(example);
            response.setTotal(Long.valueOf(total));
            response .setDetailInfoList(orderDetails);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }
        catch(Exception e){
            log.error("OrderQueryServiceImpl.queryOrderList Occur Exception :" + e);
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return response;
    }

    /**
     * 获取订单详细信息
     * @param request
     * @return
     */
    @Override
    public OrderGoodsResponse getOrderDetail(OrderGoodsRequest request) {

        OrderGoodsResponse response = new OrderGoodsResponse();
        try{
            request.requestCheck();

            //首先查询地址信息
            Example example = new Example(Address.class);
            example.createCriteria().andEqualTo("userId",request.getUserId());
            List<Address> addresses = addressMapper.selectByExample(example);
            OrderGoods orderGoods = null;
            if(addresses.size() == 1){
                orderGoods = orderMapper.getOrderGoodsResponse(request.getOrderId());
            }else{
                //如果当前用户member创建了多个address地址  则用默认地址
                orderGoods = orderMapper.getOrderGoodsResponseUnique(request.getOrderId());
            }

            List<OrderItemResponse> goodsList = orderGoods.getGoodsList();
            orderGoods.setOrderTotal(new BigDecimal(0));
            //计算订单的总价格
            for (OrderItemResponse good : goodsList) {
                BigDecimal add = orderGoods.getOrderTotal().add(good.getPrice());
                orderGoods.setOrderTotal(add);
            }
            response.setOrderGoods(orderGoods);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }
        catch (Exception e){
            log.error("OrderQueryServiceImpl.queryOrderDetail Occur Exception :" + e);
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return response;
    }
}
